package com.sjx.boundarycallback.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class VolumeView extends View {

    private Paint mPaint;
    private float mBoardWidth = 0;
    private float mWidth;
    private float mHeight;
    private float mRadius;
    private float mVolumeSlideRadius;
    private RectF rectF;
    private float sweepAngle = 0;
    private ValueAnimator mAnim;
    private int mVolumeValue = 0;
    private int mMaxVolumeValue = 10;
    private float mVolumeAngle = 360 / mMaxVolumeValue;
    private Boolean isInCreaseOrDecrease;

    public VolumeView(Context context) {
        this(context, null);
    }

    public VolumeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VolumeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        rectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mRadius = Math.min(mWidth, mHeight) / 2;
        mBoardWidth = mRadius * 0.15f;
        mVolumeSlideRadius = mRadius - mBoardWidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        float mCircleX = mWidth / 2;
        float mCircleY = mHeight / 2;
        canvas.translate(mCircleX, mCircleY);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(0);
        canvas.drawCircle(0, 0, mRadius, mPaint);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBoardWidth);
        canvas.drawCircle(0, 0, mVolumeSlideRadius, mPaint);
        mPaint.setColor(Color.WHITE);
        rectF.set(-mVolumeSlideRadius, -mVolumeSlideRadius, mVolumeSlideRadius, mVolumeSlideRadius);
        canvas.drawArc(rectF, -90, sweepAngle, false, mPaint);
        canvas.restore();
    }


    /**
     * 增大减小音量
     * @param isInCreaseOrDecrease true increase volume; false decrease volume.
     */
    public void adjustVolume(Boolean isInCreaseOrDecrease) {
        this.isInCreaseOrDecrease = isInCreaseOrDecrease;
        Boolean flag;
        flag = (mVolumeValue < mMaxVolumeValue && mVolumeValue >= 0 && isInCreaseOrDecrease) || (mVolumeValue <= mMaxVolumeValue && !isInCreaseOrDecrease && mVolumeValue > 0);
        if (flag) {
            mVolumeValue = isInCreaseOrDecrease?mVolumeValue+1:mVolumeValue-1;
            setAnim();
        }
    }

    private void setAnim() {
        if (mAnim != null && mAnim.isRunning()) {
            mAnim.cancel();
        }
            if (isInCreaseOrDecrease) {
                mAnim = ValueAnimator.ofFloat((mVolumeValue -1) * mVolumeAngle, mVolumeValue * mVolumeAngle).setDuration(200);
            } else {
                    mAnim = ValueAnimator.ofFloat((mVolumeValue+1) * mVolumeAngle, mVolumeValue * mVolumeAngle).setDuration(200);
            }
            mAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    sweepAngle = (float) mAnim.getAnimatedValue();
                    invalidate();
                }
            });
        mAnim.start();

    }

}
